<?php

namespace App\Listeners;

use App\Events\OrderShipped;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class EntrustListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     * 处理事件
     * @param  OrderShipped  $event
     * @return void
     */
    public function handle(OrderShipped $event)
    {
        $this->sendSocket();
    }

    private function sendSocket()
    {
        $type    = 'publish';
        $content = '用户 15827130942 已经下单，订单号1029383，时间2016-9-22 8:00';
        $uid = 1;
        // 推送的url地址，上线时改成自己的服务器地址
        $push_api_url = 'http://114.215.40.82:2121/';
        $post_data    = [
            'type'    => $type,
            'content' => $content,
            'to'      => $uid,
        ];

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $push_api_url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        curl_setopt($ch, CURLOPT_HTTPHEADER, ['Expect:']);
        $return = curl_exec($ch);
        curl_close($ch);
        //var_export($return);
    }
}
